package com.dommy.tab.fragment;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.dommy.tab.MainActivity;
import com.dommy.tab.R;
import com.dommy.tab.pojo.User;
import com.dommy.tab.util.UserUtil;
import com.dommy.tab.util.WaterUtil;

/**
 * 登录页
 */
public class LoginActivity extends AppCompatActivity {
    public EditText userName,passWord;
    private Button login;
    private User user;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    public void init(){
        userName = findViewById(R.id.userName);
        passWord = findViewById(R.id.passWord);
        login = findViewById(R.id.login);
        listener();
    }

    public void  listener(){
//        login
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println(userName.getText().toString());
                System.out.println(passWord.getText().toString());
                if(login()){
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                }else{
                    Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_LONG).show();
                }
            }
        });
    }
    public boolean login(){
         user = UserUtil.getUser(userName.getText().toString(),passWord.getText().toString());
        if(user != null){
//            WaterUtil.FLAG = true;
//            WaterUtil.INIT();
            return  true;
        }
        return false;
    }
}
